package cbr.resilienceManager.qmul.gemom;




import java.sql.*; 
import java.util.StringTokenizer;
import java.lang.*;

import resilienceManager.qmul.gemom.Gbroker;

/*
 * The static method used for the CBR
 * load the data from the database
 * save the data in the database 
 * 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */

public class CbrSaving {
	
	// the driver of mysql 
	private final static String driver= "com.mysql.jdbc.Driver";
	// the url of database
	private final static String url="jdbc:mysql://127.0.0.1:3306/test";
	// the username
	private final static String user="root";
	// the password
	private final static String password = "admin"; 
	
	
	/**
	 * 
	 * @param CbrData 
	 * 
	 * @exception the SQL exception
	 * */
	public static void saveState(CbrData cd){
		

		try { 
			// load the driver
			Class.forName(driver); 

			// connect with database
			Connection conn = DriverManager.getConnection(url, user, password); 

			if(!conn.isClosed()) 
			{
				//System.out.println("Succeeded connecting to the Database!");	
			}
			// the case table the broker with gbid
			String table="statetable"+cd.getTableID();
			
			Statement statement = conn.createStatement();
			
			String sql="select count(*) from "+ table;
			
			ResultSet rs = statement.executeQuery(sql);
			
			int num=0;
            //get the number of 
            if(rs.next()){
            	num=rs.getInt(1);
            }
            
            int id=num+1;
			
            float[] st=cd.getState();
            sql="insert into "+ table +"(id,s1,s2,s3,s4) values ("+id+","+st[0]
            +","+st[1]
            +","+st[2]
            +","+st[3]
            +")";
			//System.out.println(sql);
			statement.executeUpdate(sql); 

			conn.close(); 

		} catch(Exception e) { 

			e.printStackTrace(); 
		}
		
		
	}
	
	public static CbrData saveSolution(String[] sl, CbrData cd)
	{
		try { 
			// load the driver
			Class.forName(driver); 

			// connect with database
			Connection conn = DriverManager.getConnection(url, user, password); 

			if(!conn.isClosed()) 
			{
				//System.out.println("Succeeded connecting to the Database!");	
			}
			// the solution table
			String table="solutiontable"+cd.getTableID();
			
			Statement statement = conn.createStatement();
			
			String sql="select count(*) from "+ table;
			
			ResultSet rs = statement.executeQuery(sql);
			
			
			int num=0;
            //get the number of 
            if(rs.next()){
            	num=rs.getInt(1);
            }
            
            int id=num+1;
			
            
            sql="insert into "+ table +"(id,s1,s2,s3,s4,risk) values ("+id+",'"+sl[0]
            +"' ,'"+sl[1]
            +"' ,'"+sl[2]
            +"' ,'"+sl[3]
            +"',"+cd.getRisk()+")";
			//System.out.println(sql);
			statement.executeUpdate(sql); 

			conn.close(); 

		} catch(Exception e) { 

			e.printStackTrace(); 
		}
		
		cd.setSoultion(sl);
		
		return cd;
	}
	

}
